<template>
  <div class="page">
    <el-form label-width="80px" inline :model="searchFrom">
      <el-form-item label="科室名称">
        <el-input v-model="searchFrom.name"></el-input>
      </el-form-item>
      <el-form-item label="启用状态">
        <el-select v-model="searchFrom.status" placeholder="请选择启用状态" clearable style="width:100%">
            <el-option label="启用" :value="0" />
            <el-option label="禁用" :value="1" />
          </el-select>
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" @click="getDepart">搜索</el-button>
        <el-button @click="show()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border>
      <el-table-column
        prop="name"
        label="科室名称"
        width="80">
      </el-table-column>
      <el-table-column
        prop="branchCode"
        label="所在校区"
        width="150">
      </el-table-column>
      <el-table-column
        prop="status"
        label="启用状态"
        width="80">
        <template slot-scope="{row}">
          {{ row.status === '0' ? '启用' : '禁用' }}
        </template>
      </el-table-column>
      <el-table-column
        prop="introduction"
        label="描述"
        width="">
      </el-table-column>
      
      <el-table-column
        label="操作"
        width="120">
        <template slot-scope="{row}">
          <el-button type="text" @click="show(row)">修改</el-button>
          <el-popconfirm
            title="确定删除吗？"
            @confirm="deleteDepart(row.id)"
          >
            <el-button slot="reference" type="text" style="color: #F56C6C;">删除</el-button>
          </el-popconfirm>
          
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="pageSizeChange"
      @current-change="pageNoChange"
      :current-page.sync="page.pageNo"
      :page-size="page.pageSize"
      layout="total, prev, pager, next"
      :total="page.total">
    </el-pagination>

    <el-dialog
      :title="form.id ? '编辑' : '新增'"
      :visible.sync="dialogVisible"
      fullscreen
      :before-close="handleClose">
      <el-form label-position="right" label-width="80px" :model="form">
        <el-form-item label="科室名称">
          <el-input v-model="form.name" placeholder="请输入科室名称"></el-input>
        </el-form-item>
        <el-form-item label="科室代码">
          <el-input v-model="form.code" placeholder="请输入科室名称"></el-input>
        </el-form-item>
        <el-form-item label="所在校区">
          <el-select v-model="form.branchCode" placeholder="请选择校区" style="width:100%">
            <el-option label="光华" value="光华" />
            <el-option label="柳林" value="柳林" />
          </el-select>
        </el-form-item>
        
        <el-form-item label="启用状态">
          <el-select v-model="form.status" placeholder="请选择启用状态" style="width:100%">
            <el-option label="启用" value="0" />
            <el-option label="禁用" value="1" />
          </el-select>
        </el-form-item>

        <el-form-item label="描述">
          <!-- <el-input type="textarea" v-model="form.introduction" placeholder="请输入姓名"></el-input> -->
          <div ref="editor" class="text" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button :loading="loading" type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { doctorQuery, updateDepart, addDepart, getDepart, deleteDepart } from '@/api/official/official'
import { mapGetters } from 'vuex'
import E from 'wangeditor'

export default {
  computed: {
    ...mapGetters([
      'imagesUploadApi',
      'baseApi'
    ])
  },
  data() {
    return {
      searchFrom: {},
      page:{
        total:0,
        pageSize:10,
        pageNo:1,
      },
      tableData: [],
      form: {},
      dialogVisible: false,
      loading: false,
      deptList: []
    }
  },
  created() {
    // this.doctorQuery()
    this.getDepart()
  },
  methods: {
    getDepart() {
      getDepart({...this.searchFrom, page: this.page.pageNo, size: this.page.pageSize}).then(res => {
        this.tableData = res.content
        this.page.total = res.totalElements
      })
    },
    doctorQuery() {
      doctorQuery({...this.searchFrom, page: this.page.pageNo, size: this.page.pageSize}).then(res => {
        this.tableData = res.content
        this.page.total = res.totalElements
      })
    },
    pageSizeChange(val){
      this.page.pageSize = val;
      this.getDepart();
    },
    pageNoChange(val){
      this.page.pageNo = val;
      this.getDepart();
    },
    show(row = {}) {
      this.dialogVisible = true
      this.form = {...row}
      this.$nextTick(() => {
        const _this = this
        var editor = new E(this.$refs.editor)
        // 自定义菜单配置
        editor.customConfig.zIndex = 10
        // 文件上传
        editor.customConfig.customUploadImg = function(files, insert) {
          // files 是 input 中选中的文件列表
          // insert 是获取图片 url 后，插入到编辑器的方法
          files.forEach(image => {
            upload(_this.imagesUploadApi, image).then(res => {
              const data = res.data
              const url = _this.baseApi + '/file/' + data.data.path
              insert(url)
            })
          })
        }
        editor.customConfig.onchange = (html) => {
          this.form.introduction = html
        }
        editor.create()
        // 初始化数据
        editor.txt.html(this.form.introduction || '')
      })
    },
    handleAvatarSuccess(res, file) {
      // this.$set(this.form, 'storagePath', this.baseApi + '/file/' + res.data.path)
      this.$set(this.form, 'storagePath', res.data.path)
      this.$set(this.form, 'storageId', res.data.uuid)
    },
    beforeAvatarUpload(file) {
      const supportedTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/bmp', 'image/webp'];
      const isValidType = supportedTypes.includes(file.type);
      const isLt2M = file.size / 1024 / 1024 < 50;

      if (!isValidType) {
        this.$message.error('上传封面只能是图片格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传封面图片大小不能超过 50MB!');
      }
      return isValidType && isLt2M;
    },
    changeDept(dept) {
      this.form.deptName = this.deptList.find(item => item.id === this.form.deptCode).name
      this.form.branchCode = this.deptList.find(item => item.id === this.form.deptCode).branchCode
    },
    submit() {
      this.loading = true
      const data = {
        ...this.form
      }
      if (this.form.id) {
        updateDepart(data).then(res => {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.getDepart()
        }).finally(() => {
          this.loading = false
          this.dialogVisible = false
        })
      } else {
        addDepart(data).then(res => {
          this.$message({
            message: '添加成功',
            type: 'success'
          })
          this.dialogVisible = false
          this.getDepart()
        }).finally(() => {
          this.loading = false
          this.dialogVisible = false
        })
      }
    },
    deleteDepart(id) {
      deleteDepart([id]).then(res => {
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        this.getDepart()
      })
    }
  }
}
</script>

<style scoped>
.page {
  padding: 24px;
}
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9 !important;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
  border-radius: 0;
}
</style>
